{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "130754b2",
   "metadata": {},
   "source": [
    "# Export a grid model from PowerFactory to pandapower"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43eb18ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import powerfactory as pf\n",
    "from pandapower.converter.powerfactory.validate import validate_pf_conversion\n",
    "from pandapower.converter.powerfactory.export_pfd_to_pp import import_project, from_pfd\n",
    "import logging\n",
    "logger = logging.getLogger(__name__)\n",
    "logger.setLevel(\"INFO\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fece74b",
   "metadata": {},
   "source": [
    "## Make sure that the Python path contains the PowerFactory Python folder"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d602fc6a-3c3e-4b3e-8b97-3b5337a915e8",
   "metadata": {},
   "source": [
    "For example:\n",
    "C:\\\\Program Files\\\\DIgSILENT\\\\PowerFactory 2024 SP1\\\\Python\\\\3.12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9ccfccf-ac55-4cdf-be24-ff430fe3afd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ['PYTHONPATH']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6711283",
   "metadata": {},
   "source": [
    "## Start PowerFactory in Engine Mode"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "825255c3-615a-45cc-b451-4f5f1fd1f0dc",
   "metadata": {},
   "source": [
    "For this to work, make sure that your PowerFactory desktop app is closed! You can only run engine mode when the desktop app is not open simultaneously!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b0915b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "app = pf.GetApplication()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "414cfe21",
   "metadata": {},
   "source": [
    "## Export a grid model from PowerFactory"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c0db53-8ca9-41c9-8480-edfed971519a",
   "metadata": {},
   "source": [
    "For this example to work, you need to have a PowerFactory project that is called \"14 Bus System\". This is actually a standard demo in PowerFactory, which you can create from its Start Menu \"Beispiele -> 14 Knoten Netz\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abd4de78",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net = from_pfd(app, prj_name=\"14 Bus System\", handle_us=\"Nothing\", export_controller=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48e926f9",
   "metadata": {},
   "source": [
    "## Compare the power flow results in PowerFactory and pandapower"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e1b40ac-67e0-4bd3-952b-631e3e13cd73",
   "metadata": {},
   "source": [
    "Now we can compare the load flow results between the original PowerFactory model and pandapower by running:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a03212c",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_diffs=validate_pf_conversion(net, tolerance_mva=1e-9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3895c0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_diffs['diff_vm'].describe()"
   ]
  }
 ],
 "metadata": {
  "execution": {
   "allow_errors": true,
   "timeout": 300
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
